package com.cowin.inf.service.impl;

import com.cowin.base.exception.ServiceException;
import com.cowin.inf.dao.KyBdcVehicleMapper;
import com.cowin.inf.model.KyBdcVehicle;
import com.cowin.inf.model.RouteModule;
import com.cowin.inf.service.KyBdcVehicleService;
import com.cowin.base.AbstractServiceImpl;
import com.cowin.sys.model.vo.OnLineMakeVehicle;
import com.cowin.sys.service.TmVhcVehicleMovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * Created by  on 2020/03/20.
 *
 * @author CodeGenerator
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class KyBdcVehicleServiceImpl extends AbstractServiceImpl<KyBdcVehicle> implements KyBdcVehicleService {

    @Resource
    private KyBdcVehicleMapper kyBdcVehicleMapper;
    @Resource
    private TmVhcVehicleMovementService tmVhcVehicleMovementService;

    @Override
    @Async
    public void vehiclePointRecord(String vin, String mesScanNo, String decisionNode) {
        if ("W2".equals(mesScanNo) || "K2".equals(mesScanNo) || "K12".equals(mesScanNo) || "P1".equals(mesScanNo) || "P10".equals(mesScanNo) || "P11".equals(mesScanNo)) {
            List<KyBdcVehicle> findByVin = kyBdcVehicleMapper.findByVin(vin);
            if (findByVin != null && findByVin.size() > 0) {
                findByVin.get(0).setUpdateTime(new Date());
                findByVin.get(0).setPoint(mesScanNo);
                int i = kyBdcVehicleMapper.updateByPrimaryKeySelective(findByVin.get(0));
                log.info("更改BDC车辆{}状态 {}条", vin, i);
            } else {
                List<OnLineMakeVehicle> otherMessageByVinList = kyBdcVehicleMapper.findOtherMessageByVin(vin);
                if (otherMessageByVinList.size() > 0) {
                    OnLineMakeVehicle otherMessageByVin = otherMessageByVinList.get(0);
                    KyBdcVehicle kyBdcVehicle = new KyBdcVehicle();
                    if ("P10".equals(mesScanNo) && "P12".equals(decisionNode)) {
                        kyBdcVehicle.setPoint("P1");
                    } else {
                        kyBdcVehicle.setPoint(mesScanNo);
                    }
                    kyBdcVehicle.setFrameNo(otherMessageByVin.getFrameNo());
                    kyBdcVehicle.setMaterialDesc(otherMessageByVin.getMaterialDesc());
                    kyBdcVehicle.setMaterialNo(otherMessageByVin.getMaterialNo());
                    kyBdcVehicle.setSapOrderNo(otherMessageByVin.getSapOrderNo());
                    kyBdcVehicle.setState("0");
                    kyBdcVehicle.setUpdateTime(new Date());
                    kyBdcVehicle.setCreateTime(new Date());
                    kyBdcVehicle.setVin(vin);
                    int i = kyBdcVehicleMapper.insertSelective(kyBdcVehicle);
                    log.info("插入BDC车辆{}状态 {}条", vin, i);
                }
            }
        }
    }

    @Override
    public List<OnLineMakeVehicle> findByPoint(String point, String vin) {

        return kyBdcVehicleMapper.findByPoint(point, vin);
    }

    @Override
    public List<OnLineMakeVehicle> findPbs(String point, String vin) {
        return kyBdcVehicleMapper.findPbs(point, vin);
    }

    @Override
    @Async
    public void batchUpdateVehicleStatus(List<String> vins, String state) {
        log.info("更新状态BDC");
        if (vins.size() > 0) {
            kyBdcVehicleMapper.batchUpdateVehicleStatus(vins, state);
        }
    }

    @Override
    public void updateStateByPointAndVin() {
        List<String> k2 = kyBdcVehicleMapper.findByPointAndState("K2", "1");
        if (k2.size() > 0) {
            List<String> hz11_20 = tmVhcVehicleMovementService.getVinHasRecordByUlocId(k2, "HZ11_20");
            if (hz11_20.size() > 0) {
                kyBdcVehicleMapper.batchUpdateVehicleStatus(hz11_20, "0");
            }
        }
        List<String> p1 = kyBdcVehicleMapper.findByPointAndState("P1", "1");
        if (p1.size() > 0) {
            List<String> tz11_20 = tmVhcVehicleMovementService.getVinHasRecordByUlocId(p1, "TZ11_20");
            if (tz11_20.size() > 0) {
                kyBdcVehicleMapper.batchUpdateVehicleStatus(tz11_20, "0");
            }
        }
    }
}
